package com.hzncc.scaffold.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Bo.ReplyBo;
import com.hzncc.scaffold.entity.Other.Reply;
import com.hzncc.scaffold.entity.Other.User;
import com.hzncc.scaffold.entity.Vo.AskVo;
import com.hzncc.scaffold.entity.Vo.CourseVo;
import com.hzncc.scaffold.entity.Vo.PublisherVo;
import com.hzncc.scaffold.entity.Vo.ReplyVo;
import com.hzncc.scaffold.mapper.ReplyMapper;
import com.hzncc.scaffold.service.ReplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzncc.scaffold.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 回复表 服务实现类
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Service
public class ReplyServiceImpl extends ServiceImpl<ReplyMapper, Reply> implements ReplyService {

    @Autowired
    UserService userService;

    @Autowired
    ReplyService replyService;

    @Autowired
    ReplyMapper replyMapper;

    @Override
    public ResultT list(Integer pageNum, Integer pageSize, String keyword, Integer courseId, Integer status) {
        QueryWrapper<ReplyVo> qw = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)) {
            qw.like("`ask`.`content`", keyword);
        }
        if (courseId != null) {
            qw.eq("`ask->course`.`id`", courseId);
        }
        if (status != null) {
            qw.eq("`reply`.`status`", status);
        }
        ArrayList<ReplyVo> replyVoList = new ArrayList<>();

        if (pageNum != null && pageSize != null && pageNum > 0 && pageNum > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<ReplyBo> replyBoList = replyMapper.list(qw);
        for (ReplyBo replyBo : replyBoList) {
            ReplyVo replyVo = new ReplyVo();
            BeanUtil.copyProperties(replyBo, replyVo);


            AskVo ask = new AskVo();
            CourseVo course = new CourseVo();
            User creator = new User();
            if (userService.getById(replyBo.getCreatorId()) != null) {
                creator = userService.getById(replyBo.getCreatorId());
            }
            PublisherVo publisher = new PublisherVo();

            publisher.setId(replyBo.getPublisherId());
            publisher.setName(replyBo.getPublisherName());

            course.setId(replyBo.getAskCourseId());
            course.setTitle(replyBo.getAskCourseTitle());

            ask.setContent(replyBo.getAskContent());
            ask.setCourse(course);
            ask.setId(replyBo.getAskId());
            ask.setTitle(replyBo.getAskTitle());

            replyVo.setAsk(ask);
            replyVo.setContent(replyBo.getContent());
            replyVo.setCreateTime(replyBo.getCreateTime());
            replyVo.setCreator(creator);
            replyVo.setId(replyBo.getId());
            replyVo.setPubTime(replyBo.getPubTime());
            replyVo.setPublisher(publisher);

            replyVoList.add(replyVo);
        }
        PageInfo<ReplyVo> info = new PageInfo<>();
        info.setList(replyVoList);
        info.setTotal(new PageInfo<>(replyBoList).getTotal());

        return ResultT.success(info);
    }
}
